<?php if (!defined('BASEPATH'))    exit('No direct script access allowed');

class User extends CI_Controller{
    function __construct() {
        parent::__construct();       
        $usr = $_COOKIE['c_username'];
        $fname = $_COOKIE['c_fullname'];
        $id = $_COOKIE['c_iduser'];
        
        if(empty($usr)||empty($fname)||empty($id)){
            redirect('Login');
        }   
    }    

    function index() {        
        $data['title'] = "Dashboard";
        $data['tab_title'] = "Dashboard";
        $data['icon'] = "dashboard";
        $data['page'] = "dashboard";
        $this->load->view('user/home',$data);
    }
    
    function new_entry() {
        $this->load->model('m_category');
        $data['categories'] = $this->m_category->get_all_category();
        $this->load->model('m_location');
        $data['locations'] = $this->m_location->get_all_location();
        $data['title'] = "Post New Entry";
        $data['tab_title'] = "Post New Entry";
        $data['icon'] = "plus-square";
        $data['page'] = "new_entry";
        $this->load->view('user/home',$data);
    }
    function all_entries($id=0) {
        $this->load->model('m_post');
        $this->load->library('pagination');
        $this->db->select('*');
        $this->db->from('post');
        $this->db->join('user', 'user.id_user = post.id_user');
        $this->db->join('location', 'location.id_location = post.id_location');
        $this->db->join('category', 'category.id_category = post.id_category');
        $this->db->where('post.id_user', $_COOKIE['c_iduser']); 
        $jml = $this->db->get();

        //pengaturan pagination
        $config['base_url'] = site_url().'/User/all_entries';
        $config['total_rows'] = $jml->num_rows();
        $config['per_page'] = '5';
        $config['num_links'] = 2;

        $config['next_link'] = '<i class="fa fa-forward"></i>';
        $config['prev_link'] = '<i class="fa fa-backward"></i>';
        $config['first_link'] = '<i class="fa fa-step-backward"></i>';
        $config['last_link'] = '<i class="fa fa-step-forward"></i>';

        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#" style="color:#fff;">';
        $config['cur_tag_close'] = '</li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';

        //inisialisasi config
        $this->pagination->initialize($config);

        //buat pagination
        $data['halaman'] = $this->pagination->create_links();

        $data['query'] = $this->m_post->select_post_by_user($config['per_page'], $id);
        
        $data['title'] = "All Entries";
        $data['tab_title'] = "All Entries";
        $data['icon'] = "files-o";
        $data['page'] = "entries";
        $this->load->model('m_post');
        //$data['post'] = $this->m_post->select_post_by_user();

        $this->load->view('user/home',$data);
    }
    
    function profile() {
        $data['username'] = $_COOKIE['c_username'];
        $data['password'] = $_COOKIE['c_password'];
        $this->load->model('m_account');
        $data['db_result'] = $this->m_account->select_a_record($data);
        $data['title'] = "Profile";
        $data['tab_title'] = "Your Profile";
        $data['icon'] = "photo";
        $data['page'] = "profile";        
        $this->load->view('user/home',$data);
    }
    
    function edit_profile() {
        $data['title'] = "Edit Profile";
        $data['tab_title'] = "Edit Your Profile";
        $data['icon'] = "edit";
        $data['page'] = "edit_profile";
        
        $this->load->model('m_user');
        $data['user'] = $this->m_user->select_user_byId($_COOKIE['c_iduser']);
        
        if($this->input->post('btn_update_user') != ""){
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('edit_fullname', 'Full Name', 'min_length[6]|max_length[50]');
            $this->form_validation->set_rules('edit_password', 'Password');
            $this->form_validation->set_rules('edit_2ndpassword', 'Re-type Password','matches[edit_password]');
            $this->form_validation->set_rules('new_ava', 'New Avatar');
            
            if ($this->form_validation->run() != FALSE){
                if($this->input->post('edit_fullname') != ""):
                    $user_data['fullname'] = $this->input->post('edit_fullname');
                else:
                    $user_data['fullname'] = $data['user']['fullname_user'];
                endif;
                
                if($this->input->post('edit_password') != ""):
                    $user_data['new_password'] = md5($this->input->post('edit_password'));
                else:
                    $user_data['new_password'] = $data['user']['password_user'];
                endif;
                
                if($this->input->post('edit_fb') != ""):
                    $user_data['fb'] = $this->input->post('edit_fb');
                else:
                    $user_data['fb'] = $data['user']['fb_acc'];
                endif;
                
                if($this->input->post('edit_twitter') != ""):
                    $user_data['twitter'] = $this->input->post('edit_twitter');
                else:
                    $user_data['twitter'] = $data['user']['twitter_acc'];
                endif;

                if($this->input->post('edit_gplus') != ""):
                    $user_data['gplus'] = $this->input->post('edit_gplus');
                else:
                    $user_data['gplus'] = $data['user']['gplus_acc'];
                endif;

                if($this->input->post('edit_bio') != ""):
                    $user_data['bio'] = $this->input->post('edit_bio');
                else:
                    $user_data['bio'] = $data['user']['bio'];
                endif;
                
                $new_name = date('Ymd-his');
                $this->load->library('upload');
                $config['upload_path'] = './Images/User_Avatar/'; 
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '800';
                $config['file_name'] = "UserAva-$new_name";

                $this->upload->initialize($config);
                if (!$this->upload->do_upload('new_ava')) {
                    $upld = array('message' => $this->upload->display_errors());
                } else {
                    $upld = array('message' => $this->upload->data());
                }
                if(isset($upld['message']['file_name'])){
                    $user_data['ava'] = $upld['message']['file_name'];
                }

                //else:
                  //  $user_data['new_ava'] = $data['user']['user_ava'];
                //endif;

                $this->load->model('m_user');
                $cek = $this->m_user->update_user($user_data);
                
                if($cek ==  TRUE):
                    $data['message'] = "1";
                else:
                    $data['message'] = "0";
                $data['error'] = "Update Profile Failed";
                endif;
                
            }else{
                $data['message'] = "0";
                $data['error'] = validation_errors();
            }
        }
        $this->load->view('user/home',$data);
    }
}
